
export default {
  methods: {
    keyupSubmit(e) {
      const _key = e.keyCode;

      if (_key === 13) {
        // 如果是下拉框不执行
        const className = e.target?.parentNode?.parentNode?.className || "";
        if (className.includes("el-select")) {
          console.log("下拉框");
          return;
        }

        let IS_OPEN_NUM = 0;
        // 弹框组件的显示层级
        let dialogWrapperzIndex = 0;
        let modelTitle = "*";

        // 查看有没有表单组件是打开的 el-drawer__wrapper
        const dialogWrapper = document.querySelectorAll(".el-dialog__wrapper");
        if (dialogWrapper.length) {
          dialogWrapper.forEach(item => {
            const { display, zIndex } = item.style;
            if (!display) {
              IS_OPEN_NUM++;
              if (zIndex > dialogWrapperzIndex) {
                dialogWrapperzIndex = zIndex;
                modelTitle = item.title;
              }
            }
          });
        }

        // 有无 el-drawer 弹框打开
        const drawerWrapper = document.querySelectorAll(".el-drawer__wrapper");
        let drawerWrapperCount = 0;
        if (drawerWrapper.length) {
          drawerWrapper.forEach(item => {
            const { display } = item.style;
            if (!display) {
              drawerWrapperCount++;
            }
          });
        }
        // 如果没有弹框打开 直接执行最外层的 查询方法
        if (!IS_OPEN_NUM && !drawerWrapperCount) {
          // 使用的是查询表单
          if (this.formType === "search" && !this.visible) {
            this.handleConfirm();
          }
        } else {
          // 只执行弹框是打开的查询
          if (this.visible && this?.$parent.title === modelTitle) {
            this.handleConfirm();
          }
        }
      }
    },
    useEnter() {
      window.addEventListener("keydown", this.keyupSubmit, false);
      this.$once("hook:beforeDestroy", () => {
        window.removeEventListener("keydown", this.keyupSubmit, false);
      });
    }
  },

  mounted() {
    this.useEnter();
  }
};
